#coding:utf8
'''
Created on 2011-6-13

@author: mark
'''

import config

db = config.database
mc = config.memcache_client

def getAllDistricts():
    key = str("alldistricts")
    allDistricts = mc.get(key)
    if not allDistricts:
        allDistricts = _getAllDistricts()
        if allDistricts:
            mc.set(key, allDistricts, config.cache_timeout)
    return allDistricts

def _getAllDistricts():
    allDistricts = db.select("district")
    return allDistricts

def getDistrictById(id):
    key = str("district_%s" % id)
    district = mc.get(key)
    if not district:
        district = _getDistrictById(id)
        if district:
            mc.set(key, district, config.cache_timeout)
    return district

def _getDistrictById(id):
    district = db.select("district", where="id = %s" % id)
    return district
